<template>
  <div class="container">
    <div class="top">
      <img :src="base_url+'/mp_3.0/bg.png'" alt class="img" mode="widthFix" />
      <div class="logo">
        <img :src="base_url+'/mp_3.0/logo.png'" alt class="logo-icon" />
      </div>
    </div>
    <div class="title">美美咖+</div>
    <div class="title-2">请先绑定手机号</div>
    <div class="btn-bar">
      <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="get_tel">绑定手机号</button>
    </div>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data() {
    return {
      base_url: common.image_resource,
      real_tel: "",
      user_jwt: "",
      source: ""
    };
  },
  methods: {
    get_tel(e) {
      console.log(e);

      let that = this;
      let s_data = wx.getStorageSync("s_data");
      if (e.mp.detail.errMsg == "getPhoneNumber:ok") {
        //同意获取手机号
        wx.showLoading({
          title: "", //提示的内容,
          mask: true, //显示透明蒙层，防止触摸穿透,
          success: res => {}
        });
        wx.login({
          success: result => {
            //手机号转换
            //通过手机查上下级状态
            common.fly_post(
              "api/v4_2/super_code/get_phone_bycode",
              {
                iv: e.mp.detail.iv,
                code: result.code,
                encryptedData: e.mp.detail.encryptedData,
                status: 1,
                source: this.source,
                user_jwt: this.user_jwt
              },
              response => {
                let res = response.data;
                wx.hideLoading();
                if (res.status_code == 0) {
                  wx.hideLoading();
                  wx.hideLoading();
                  console.log(res);
                  this.real_tel = res.data.phone;
                  this.bind_tel(res.data.phone);
                }
              }
            );
            // wx.setStorageSync("loginobj", objset);
            // wx.navigateTo({
            //   url: "/pages/recommend/main"
            // });
          }
        });
      }
    },
    bind_tel(phone) {
      let that = this;
      wx.showLoading({
        title: "", //提示的内容,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => {}
      });
      //通过手机查上下级状态
      common.fly_post(
        "api/v4_2/super_code/get_is_superior",
        {
          status: 2,
          phone: phone,
          source: this.source,
          user_jwt: this.user_jwt
        },
        response => {
          let res = response.data;
          var obj = {
            status: 2,
            phone: phone,
            source: this.source,
            user_jwt: this.user_jwt
          };
          wx.setStorageSync("loginobj", obj);
          wx.hideLoading();
          if (res.status_code == 0) {
            console.log(res);
            if (res.data.is_superior == 0) {
              //没有上级关系
              wx.navigateTo({
                url: "/pages/package_main/recommend/main"
              });
            } else {
              this.oldfun();
            }
          }
        }
      );
    },
    oldfun() {
      common.fly_post(
        "api/v4_2/minapp/bind_mobile",
        {
          status: 2,
          phone: this.real_tel,
          source: this.source,
          user_jwt: this.user_jwt
        },
        response => {
          let res = response.data;
          wx.hideLoading();
          if (res.status_code == 0) {
            wx.setStorageSync("user_token", res.data.token);
            wx.setStorageSync("user_id", res.data.user_id);
             wx.setStorageSync("account_type", res.data.user.data.account_type);
            wx.setStorageSync("role_id", res.data.user.data.role_id);
            wx.setStorageSync("is_plus", res.data.user.data.is_plus);
            wx.setStorageSync(
              "is_home_plus_entrance",
              res.data.user.data.is_home_plus_entrance
            );
            //  that.bind_user();
            wx.showToast({
              title: "登录成功", //提示的内容,
              icon: "success", //图标,
              duration: 1500, //延迟时间,
              mask: true, //显示透明蒙层，防止触摸穿透,
              success: res => {}
            });
            if (res.data.loginType == 0) {
              //注册
              wx.navigateTo({
                url: "/pages/package_main/recommend/main"
              });
            } else {
              //已经注册
              /**分享过来要跳转到相对应的页面的方法 */
              let s_data = wx.getStorageSync("s_data");
              wx.getStorage({
                key: "jump_url",
                success: result => {
                  console.log(result.data);
                  if (s_data.t == 13) {
                    wx.reLaunch({
                      url: result.data + "&share_id=" + s_data.sid
                    });
                    console.log(result.data + "&share_id=" + s_data.sid);
                  } else {
                    wx.redirectTo({ url: result.data });
                  }
                  // wx.redirectTo({ url: result.data });
                  wx.removeStorage({
                    key: "jump_url",
                    success: res => {}
                  });
                },
                fail: () => {
                  //   绑定成功  返回到上一级
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 2 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                    });
                  }, 1500);
                },
                complete: () => {}
              });
            }
          } else {
            wx.showToast({
              title: res.message, //提示的内容,
              icon: "none", //图标,
              duration: 1500, //延迟时间,
              mask: false //显示透明蒙层，防止触摸穿透,
            });
          }
        }
      );
    }
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "美美咖"
    });
    wx.setNavigationBarColor({
      frontColor: "#ffffff", //前景颜色值，包括按钮、标题、状态栏的颜色，仅支持 #ffffff 和 #000000,
      backgroundColor: "FB6C88", //背景颜色值，有效值为十六进制颜色,
      success: res => {}
    });
    console.log(options);
    if (options.user_jwt) {
      this.user_jwt = options.user_jwt;
      wx.setStorageSync("user_login", options);
      this.source = options.source;
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  .top {
    width: 100%;
    position: relative;
    .img {
      width: 100%;
      display: block;
    }
    .logo {
      width: 140px;
      height: 140px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 2px 34px 0px rgba(233, 60, 80, 0.23);
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translate(-50%, 50%);
      .logo-icon {
        width: 111px;
        height: 111px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }
  .title {
    font-size: 24px;
    font-family: "PingFangSC-Medium", "PingFangSC";
    font-weight: 500;
    color: rgba(255, 56, 62, 1);
    line-height: 30px;
    margin-top: 85px;
    text-align: center;
  }
  .title-2 {
    font-size: 26px;
    font-family: "PingFangSC-Semibold", "PingFangSC";
    font-weight: 600;
    color: rgba(51, 51, 51, 1);
    line-height: 32px;
    text-align: center;
    margin-top: 30px;
  }
  .btn-bar {
    position: absolute;
    bottom: 50px;
    left: 50%;
    transform: translateX(-50%);
    .login-btn {
      width: 255px;
      height: 50px;
      background: rgba(2, 187, 0, 1);
      border-radius: 5px;
      border-radius: 8px;
      font-size: 18px;
      font-family: "PingFangSC-Medium", "PingFangSC";
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
      line-height: 50px;
      text-align: center;
    }
    .back-btn {
      width: 255px;
      height: 50px;
      background: rgba(241, 241, 241, 1);
      border-radius: 8px;
      font-size: 18px;
      font-family: "PingFangSC-Regular", "PingFangSC";
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      line-height: 50px;
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>
